# -*- coding: utf8 -*-
""" 本部分只能处理东北半球的北斗网格码，其他部分暂不支持 """
from .beidou_northeast_hemisphere import transform_longitude_latitude
from .beidou_northeast_hemisphere import beidou_grid_encoding
from lat_lon_parser import parse


def beidou_grid_code_point(longitude, latitude, level_n):
    """
    本部分只能处理东北半球的北斗网格码，其他部分暂不支持.维度范围在 [0,88)
    :param point: (longitude, latitude)
    :return: str
    """
    point = (longitude,latitude)
    try:
        longitude_float = parse(longitude)
        latitude_float = parse(latitude)
    except:
        raise Exception(f'point format error,please check point {point}')
    else:
        try:
            assert longitude_float >= 0 and latitude_float >= 0
        except:
            raise Exception(f'只能处理东北半球的经纬度点，{point}')
    # 转化经纬度为实数值
    longitude_std = transform_longitude_latitude(longitude)
    latitude_std = transform_longitude_latitude(latitude)
    grid_code = beidou_grid_encoding(longitude_std, latitude_std, level_n)
    return grid_code


if __name__ == '__main__':
    # 经度 - x 轴 东经
    longitude = 'E116°18′45.37″'
    # 维度 - y 轴 北半球
    latitude = 'N39°59′35.38″'
    for i in range(1, 11, 1):
        result_level_n = beidou_grid_code_point(longitude, latitude, i)
        print('level i -{}- result is'.format(i), result_level_n)
